
clear
do "...\First.do"

********************************************************************************

clear
use "$work\full_sample.dta"

keep if inrange(age,40,70)
keep if inrange(year,1995,2019)
sort pnr year

********************************************************************************
** GRAPH
********************************************************************************

destring udd_level, g(udd_kode)
tab udd_kode

g udd_group=1 if inlist(udd_kode,10) 
replace udd_group=2 if inlist(udd_kode,15,20,25) 
replace udd_group=3 if inlist(udd_kode,30,35) 
replace udd_group=4 if udd_kode==40 
replace udd_group=5 if udd_kode==50 
replace udd_group=6 if udd_kode>50 
replace udd_group=. if udd_kode==. 

lab define udd_group2 1 "Primary education" 2 "High School" 3 "Vocational Education" 4 "Short tertiary education" 5 "College" 6 "University"
lab values udd_group udd_group2


reg death i.age male i.year
predict res, res

egen base=mean(death)

g res_base=res+base
bys udd_group: sum res_base
bys low_ses: sum res_base

bys udd_group: egen mean_PCP_SES=mean(yder_ses_max)
bys udd_group yder_ses_max: egen mean_SES_death_res=mean(res)

bys low_ses: egen mean_PCP_SES2=mean(yder_ses_max)
bys low_ses yder_ses_max: egen mean_SES_death_res2=mean(res)

replace mean_SES_death_res=mean_SES_death_res+base
replace mean_SES_death_res=mean_SES_death_res*100

replace mean_SES_death_res2=mean_SES_death_res2+base
replace mean_SES_death_res2=mean_SES_death_res2*100


keep udd_group mean_PCP_SES mean_SES_death_res yder_ses_max low_ses mean_PCP_SES2 mean_SES_death_res2
duplicates drop

tab mean_SES_death_res2 if udd_group==1 
tab mean_SES_death_res yder_ses_max if udd_group==1
tab mean_SES_death_res2 low_ses if yder_ses_max==1
tab mean_SES_death_res2 low_ses if yder_ses_max==0

tab udd_group mean_SES_death_res if yder_ses_max==1
tab udd_group mean_SES_death_res if yder_ses_max==0


drop mean_SES_death_res2 mean_PCP_SES2 low_ses
duplicates drop

tw (bar mean_SES_death_res udd_group if yder_ses_max==0, fcolor(gs0%40) lcolor(gs0%40)) (bar mean_SES_death_res udd_group if yder_ses_max==1, fcolor(white%40) lcolor(black) graphregion(color(white) ilcolor(white) lcolor(white)) bgcolor(white) ytitle("Mortality %") xtitle(" " "Patient education")  ///
xlabel(1 `" "Primary" "Education" "' 2 `" "High" "School" "' 3 `" "Vocational" "Education" "' 4 `" "Short Tertiary" "Education" "' 5 "College" 6 "University", labsize(medsmall)) legend(order(1 "High-SES Physician" 2 "Low-SES Physician") pos(2) ring(0) col(1) lstyle(none) nobox region(lstyle(none)))) 
graph export "$fig\Match_PCPpatient_mortality_bw.png", replace 
graph export "$fig\Match_PCP_patient_mortality_bw.pdf", replace 

